<?php
class Profile_m extends MY_Model{
	
	function __construct(){
		parent::__construct();
		$this->_table = 'emp_profile';
		
	}
	function get_by_room($room_id,$childs = NULL){
		$this->_table = 'emp_profile p';
		$this->db->select('u.display_name,u.group_id,p.*,r.title as room_title ');
		$this->db->from($this->_table);
		$this->db->join('ser_users u','u.id = p.id');
		$this->db->join('emp_room r','r.id = p.room_id');
		$this->db->where('u.active',1);
		$this->db->where('p.room_id',$room_id);
		//sap xep theo hop dong status
		$this->db->order_by('p.status');
		$this->db->order_by('r.id');
		$this->db->order_by('u.group_id');
		if($childs)
			$this->db->or_where('p.room_id in ('.$childs.')');
		//return parent::get_all();
		return $this->db->get()->result();
	}
	
	public function get_by_many($params = array()){
		$this->_table = 'emp_profile p';
		$this->db->select('u.display_name,u.group_id,p.*,r.title as room_title ');
		$this->db->from($this->_table);
		$this->db->join('ser_users u','u.id = p.id');
		$this->db->join('emp_room r','r.id = p.room_id');
		$this->db->where('u.active',1);
		if(!empty($params['group_id']))
			$this->db->where('u.group_id',$params['group_id']);
		$this->db->order_by('r.id');
		$this->db->order_by('u.group_id');
		//if($childs)
		//	$this->db->or_where('p.room_id in ('.$childs.')');
		return $this->db->get()->result();
	}
	
	public function get_all($param = array()){
		$this->_table = 'emp_profile p';
		$this->db->select('u.display_name,u.group_id,u.email,p.*,r.title as room_title ');
		$this->db->from($this->_table);
		$this->db->join('ser_users u','u.id = p.id');
		$this->db->join('emp_room r','r.id = p.room_id');
		if(!empty($param['active']))
			$this->db->where('u.active',$param['active']);
		$this->db->order_by('u.group_id');
		return $this->db->get()->result();
	}
	
	public function get($id){
		$this->_table = 'emp_profile p';
		$this->db->select('u.display_name,u.email,u.avatar,u.group_id,p.*');
		$this->db->join('ser_users u','u.id = p.id');
		$this->db->where('p.id',$id);
		return $this->db->get($this->_table)->row();
	}
	
	public function background($id,$color){
		parent::update($id,array('background'=>$color));
	}
	
	//convert commas to number
	private function convert_to_double($value = ''){
		return floatval(str_replace(',', '', str_replace('.', '', $value)));
	}
	
	public function update_salary($salary){
		//user_id, salary
		foreach($salary as $user_id=>$value){
			$value = $this->convert_to_double($value);
			parent::update($user_id,array('salary'=>$value));
		}
	}
	public function update_salary2($salary){
		//user_id, salary
		foreach($salary as $user_id=>$value){
			$value = $this->convert_to_double($value);
			parent::update($user_id,array('salary2'=>$value));
		}
	}
	public function update_factor($factor){
		//user_id, factor
		foreach($factor as $user_id=>$value){
			$value = $this->convert_to_double($value);
			parent::update($user_id,array('factor'=>$value));
		}
	}
	public function update_s1($factor){
		foreach($factor as $user_id=>$value){
			$value = $this->convert_to_double($value);
			parent::update($user_id,array('s1'=>$value));
		}
	}
	public function update_s2($factor){
		//user_id, factor
		foreach($factor as $user_id=>$value){
			$value = $this->convert_to_double($value);
			parent::update($user_id,array('s2'=>$value));
		}
	}
	public function create_id2(){
		$l = '00000';
		$profiles = parent::get_all();
		foreach($profiles as $p){
			$id = $p->id;
			$id2 = substr($l.$id,-strlen($l));
			parent::update($id,array('id2'=>$id2));
		}
	}
}